<template>
<el-dialog class="add-dialog" :title="addForm.title" :visible.sync="addForm.show" width="500px">
    <el-form ref="ruleForm" :model="addForm.form" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" prop="name">
            <el-input clearable placeholder="请输入项目名称" v-model="addForm.form.name"></el-input>
        </el-form-item>
        <el-form-item label="项目代码" prop="code">
            <el-input clearable placeholder="请输入项目代码" v-model="addForm.form.code"></el-input>
        </el-form-item>
        <el-form-item label="项目描述">
            <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" clearable placeholder="请输入项目描述" v-model="addForm.form.desc"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addForm.show=false">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    name: 'AddDialog',
    props: {
        addForm: {
            type: Object,
            required: true
        }
    },
    watch: {
        addForm: {
            handler (a) {
                if (!a.show) {
                    let obj = this.$refs['ruleForm']
                    obj && obj.resetFields()
                }
            },
            deep: true
        }
    },
    data () {
        return {
            list: [{
                value: '0',
                name: '区域一'
            },
            {
                value: '1',
                name: '区域二'
            },
            {
                value: '2',
                name: '区域三'
            }
            ],
            rules: {
                name: [
                    { required: true, message: '请输入项目名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入项目代码', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        handleSave () {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.$emit('save')
                } else {
                    return false
                }
            })
        }
    }
}
</script>

<style lang="less">
.add-dialog {}
</style>
